﻿using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Json;
using System.Text;

namespace AT.IBMS.Framework.Tools
{
    public static class SerializeToolcs
    {

        /// <summary>
        /// 序列化。
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string Serialize(this Tool t, object obj)
        {

            MemoryStream memoryStream;
            BinaryFormatter serializer;
            byte[] buffer;

            using (memoryStream = new MemoryStream())
            {

                serializer = new BinaryFormatter();
                serializer.Serialize(memoryStream, obj);
                buffer = memoryStream.ToArray();
            }

            return Convert.ToBase64String(buffer);
        }

        /// <summary>
        /// 反序列化。
        /// </summary>
        /// <param name="serializationString"></param>
        /// <returns></returns>
        public static object Deserialize(this Tool t, string serializationString)
        {

            byte[] buffer;
            BinaryFormatter serializer;
            MemoryStream memoryStream;
            object obj;

            buffer = Convert.FromBase64String(serializationString);

            using (memoryStream = new MemoryStream(buffer))
            {
                serializer = new BinaryFormatter();
                obj = serializer.Deserialize(memoryStream);
            }

            return obj;

        }

        /// <summary>
        /// 把类型序列化成json字符串。
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string GetJsonText<T>(this Tool t, T obj) where T : class
        {

            MemoryStream stream;
            DataContractJsonSerializer json;
            string jsonText;

            jsonText = "";
            json = new DataContractJsonSerializer(obj.GetType());

            using (stream = new MemoryStream())
            {
                json.WriteObject(stream, obj);
                jsonText = Encoding.UTF8.GetString(stream.ToArray());
            }

            return jsonText;

        }

        /// <summary>
        /// 把json字符串反序列化成指定的类型。
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="jsonText"></param>
        /// <returns></returns>
        public static T ParseFromJson<T>(this Tool t, string jsonText) where T : class
        {

            MemoryStream ms;
            DataContractJsonSerializer serializer;
            T result;

            using (ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonText)))
            {
                serializer = new DataContractJsonSerializer(typeof(T));
                result = serializer.ReadObject(ms) as T;
            }

            return result;

        }

    }
}
